

###########################################################
##### Haiti elite network project  		          			#####
##### master code			 			                      		#####
##### 2021 mar 03                   									#####
###########################################################


## clear workspace
rm(list=ls())

## set working directory
setwd('~/Dropbox/Haiti_trade/ReplicationPkg')


##########
## functions
##########

source('02_Code/functions.R')



##########
## clean family and network data
##########

# business ownership
  # read in list of names and types from trade data
  # create last name variable
  # match list of comp names to corp data
  # match list of ind names to genealogy

source("02_Code/01_Cleaning/clean_ownership.R")

# political / military elite
  # create cohort var
  # create measures of qual and quant of pol rank
  # create measures of qual and quant of mil rank
  # create regime measure
  # read in coup plotters list
  # merge biz wtih fams
  # write list of fam coup plotters

source("02_Code/01_Cleaning/clean_polbios.R")

# create individual genealogy graph
  # read in list of names and types from trade data
  # create last name variable
  # match list of comp names to corp data
  # match list of ind names to genealogy
  # match list of ind names to pol bios
  # match list of ind names to mil bios

source("02_Code/01_Cleaning/clean_gen.R")

# create family-business-product crosswalk
  # create database of family-business matches
  # merge with business-product matches from agemar
  # merge in supply prices to calculate value

source('02_Code/01_Cleaning/clean_fambizprod_xwalk.R')

# create family graph
  # collapse network data to family
  # merge in characteristics (biz, coup)
  # calculate network stats

source("02_Code/01_Cleaning/clean_gen_fam.R")
source("02_Code/01_Cleaning/clean_gen_fam_older.R")


# build dataset of product characteristics

source("02_Code/01_Cleaning/clean_product_chars.R")


##########
## build final datasets
##########

# build final family dataset
  # make a matrix of all links between last names, biz, and hs codes
  # collapse that down to the fam, firm, and four-digit hscode
  # output data for all families ('allfams.csv') and business owning families ('fam.csv')

source("02_Code/01_Cleaning/build_fam.R")


# build price time series dataset
  # merge family data into ihsi price data
  # merge in product characteristics
  # merge in supply prices
  # build some necessary variables
  # calculate missingness
  # output table of data for all CPI fam-prod pairs ('fam_con_prod_final.csv') 
  #   and price time series ('price_final.csv')

source("02_Code/01_Cleaning/build_prod.R")


##########
## analysis
##########

# which families participate in coups?

source("02_Code/02_Analysis/analysis_fam.R")

source('02_Code/02_Analysis/dataviz_fam.R')

# do the same firms trade in 2009 and 2011?

source('02_Code/02_Analysis/analysis_cpi_continuity.R')

# do families that participate benefit?

source("02_Code/02_Analysis/analysis_cpi.R")











